跳到主要内容

输出数组所有排序组合

// 输出数组内所有排序组合,比如输入[1, 2, 3],输出[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
const sortArray = (arr) => {
const result = []
const dfs = (path, arr) => {
if (path.length === arr.length) {
result.push(path)
return
}
for (let i = 0; i < arr.length; i++) {
if (path.includes(arr[i])) continue
dfs([...path, arr[i]], arr)
}
}
dfs([], arr)
return result
}